package dk.ucn.datamatiker.henrik.bundtkage.earthcake.albeheweather;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class LocationsListActivity extends Activity {
	ArrayAdapter<Location> locationsAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_locations_list);

		final ListView locationsList = (ListView) findViewById(R.id.locationList);

		try {
			locationsAdapter = new ArrayAdapter<Location>(this,
					android.R.layout.simple_list_item_1, States.getInstance()
							.getLocations());
			// locationsAdapter = new ArrayAdapter<String>(this,
			// android.R.layout.simple_list_item_1, testList);

			locationsList.setTextFilterEnabled(true);
			locationsList.setAdapter(locationsAdapter);

			locationsList.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					Object obj = locationsList.getItemAtPosition(position);
					States.getInstance().setCurrentLocation((Location) obj);

					openLocalWeatherActivity(view);

				}

				@SuppressWarnings("unused")
				public void onClick(View arg0) {
					// TODO Auto-generated method stub

				}
			});
		} catch (Exception e) {
			TextView chooseLocation = (TextView) findViewById(R.id.chooseLocationView);
			chooseLocation
					.setText("Couldn't find any locations, please try again");
		}

	}

	private void openLocalWeatherActivity(View v) {
		Intent intent = new Intent(this, AlBeHeWeatherActivity.class);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.locations_list, menu);
		return true;
	}

}
